package com.foot.travel.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.foot.travel.R;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.SubmitOrder;
import com.koalac.ironman.net.pojo.response.PayResultEvent;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.net.pojo.response.SchoolTravelDetail;
import com.koalac.ironman.util.BeeCloudUtil;
import com.koalac.ironman.viewhelper.ViewInject;
import com.koalac.ironman.widget.LoadingDialog;
import com.squareup.otto.Subscribe;

public class PayOrderActivity extends ModuleActivity implements OnClickListener{

    @ViewInject(id = R.id.route)
    TextView mRouteView;
    @ViewInject(id = R.id.date)
    TextView mDateView;
    @ViewInject(id = R.id.number)
    TextView mNumberView;
    
    @ViewInject(id = R.id.unit_price)
    TextView mUnitPriceView;
    @ViewInject(id = R.id.total_price)
    TextView mTotalPriceView;
    @ViewInject(id = R.id.down_payment)
    TextView mDownPaymentView;
    
    @ViewInject(id = R.id.school)
    TextView mSchoolView;
    @ViewInject(id = R.id.class_name)
    TextView mClassNameView;
    @ViewInject(id = R.id.linkman_name)
    TextView mLinkNameView;
    
    @ViewInject(id = R.id.linkman_phone)
    TextView mLinkPhoneView;
    @ViewInject(id = R.id.mail)
    TextView mMailView;
    @ViewInject(id = R.id.pay_order_btn,click="onClick")
    TextView mPayOrderView;
    
    private SchoolTravelDetail mSchoolTravelDetail;
    SubmitOrder mSubmitOrder;
    protected String mOrderId;
    public static final String SUBMIT_ORDER = "submit_order";
    BeeCloudUtil mBeeCloudUtil;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setNavigationTitle("订单信息");
	setBackButtonVisibility(true);
	mSubmitOrder = (SubmitOrder) getIntent().getSerializableExtra(SUBMIT_ORDER);
	mSchoolTravelDetail = (SchoolTravelDetail) getIntent().getSerializableExtra(SubmitOrderActivity.SCHOOL_TRAVLE_DETAIL);
	initData();
	mBeeCloudUtil = new BeeCloudUtil(PayOrderActivity.this); 
    }
    
    private void initData() {
	mRouteView.setText(mSchoolTravelDetail.getName());
	
	
	mDateView.setText(mSchoolTravelDetail.getDateline());
	mNumberView.setText(mSubmitOrder.getNumber());
	
	mUnitPriceView.setText(mSubmitOrder.getPerPrice());
	mTotalPriceView.setText(mSubmitOrder.getTotalPrice());
	mDownPaymentView.setText(mSubmitOrder.getFrontMoney());
	
	mSchoolView.setText(mSubmitOrder.getSchool());
	mClassNameView.setText(mSubmitOrder.getClassName());
	mLinkNameView.setText(mSubmitOrder.getContactName());
	
	mLinkPhoneView.setText(mSubmitOrder.getContactPhone());
	mMailView.setText(mSubmitOrder.getEmail());
    }

    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.activity_pay_order);
    }
    @Subscribe
    public void onReceivePayResult(PayResultEvent payResultEvent){
	if (payResultEvent.isPaySuccess()) {
	    Intent intent = new Intent(PayOrderActivity.this,PayFinishActivity.class);
	    intent.putExtra(PayFinishActivity.ORDER_ID, mOrderId);
	    startActivity(intent);
	}
    }

    @Override
    public void onClick(View v) {
	int viewID = v.getId();
	switch (viewID) {
	case R.id.pay_order_btn:
	    final LoadingDialog p = showLoadingDialog("正在提交订单...");
	    MyRequest.request(PayOrderActivity.this, mSubmitOrder, new ResponseCallback() {
	        
	        @Override
	        public void process(boolean result, Context context, Response data,
	    	    String msg) {
	            canceledLoadingDialog(p);
	            if (result) {
	        	String payOrderId = data.getObject().optString("book_id");
	        	mOrderId = data.getObject().optString("id");
	        	double perPrice = Double.parseDouble(mSubmitOrder.getFrontMoney());
	        	
	        	 mBeeCloudUtil.alipay(mSchoolTravelDetail.getName(), ((int)perPrice * 100), payOrderId);
		    }else {
			toast("提交订单失败!");
		    }
	         
	        }
	    });
	    break;

	default:
	    break;
	}
    }

}
